<?php 

namespace Home\Controller;
use Think\Controller;
	class JieController extends CommonController{
		//问题详情页
	   public function index()
	   {	
	   	$id =I('get.id');
	  
	   	//展示问题和用户的内容
		$question = M('question')->find($id);
		$uid = $question['uid'];
		$user = M('user')->find($uid);
		$this->assign('question',$question);
		$this->assign('user',$user);
		// var_dump($user);		
		$this->autoAdd('question','view_num','+1',$id);
		//获取热门条数
		$this->getHotView();


		$this ->getHotanswer();

		$sql = "select *,t1.create_time,t1.id,(select id from zan where aid=t1.id and uid='{$_SESSION['id']}') is_zan from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid={$id}";

		// var_dump($sql);exit;
		$answer= M()->query($sql);

		// foreach ($answer as $k => $v) {
		// 	$answer[$k]['is_zan'] = M('zan')->where(array('aid'=>$v['id'],'uid'=>$_SESSION['id']))->find();
		// }

		// var_dump($answer);exit;

		$this->assign('answer',$answer);







		$uid = $_SESSION['id'];
		if (M('store')->where(array('qid'=>$id,'uid'=>$uid))->find()) {
			$store =1;
		}else{
			$store=0;
		}
		$this->assign('store',$store);
		$this->display();
	   }

	   //

	   public function uploadImage()
	   {
	   		//在编辑框其中加入图片
	   		$upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Pic/'; // 设置附件上传根目录
		    $upload->savePath  =     ''; // 设置附件上传（子）目录
		    // 上传文件 
		    $info   =   $upload->upload();
		     if (!$info) {
		     	 $this->error($upload->getError());
		     }else{
		     	$path =current($info);
		     	 $path = __ROOT__."/Pic/".$path['savepath'].$path['savename'];
		     	$arr = array(
	    		'code'=>0,
	    		'msg'=>'成功',
	    		'data'=>array(
	    			'src'=>$path,
	    			'title'=>'图片',
	    			)
	    		);
	    		echo json_encode($arr);
		     }
	   }

	   //回答问题
	public function answerAdd()
  	{
	   	$data = I('post.');
	   	$id = $data['qid'];
    	$data['uid']=$_SESSION['id'];
    	$data['create_time']=time();
    	if (M('answer')->add($data)) {
    			$arr = array(
    			'error'=>0,
    			'info'=>'回复成功',
    			'url'=>U('home/jie/index',array('id'=>$id))
    			);
    			$this->autoAdd('question','answer_num','+1',$id);
    			$this->autoAdd('user','answer_num','+1',$_SESSION['id']);
    		echo json_encode($arr);
    		// var_dump($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'回复失败'
    			);
    		echo json_encode($arr);
    		exit();
    	}
	}
	// public function addShow(){
		
	// 	$id =I('get.id');
	// 	$sql = "select *,t1.create_time from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid={$id}";
	// 	$answer = M()->query($sql);
	// 	$this->assign('answer',$answer);
	// 	$this->index();

	// }

	   	//问题发布页面
	   	public function add()
	   {
	   		//取人类验证问题并且分配数据
	   		$res = D('verify')->getOneverify();
	   		$this->assign('res', $res);
	   		//去数据库表category的数据分配数据
	   		$cates = D('category')->getCate();
	   		$this->assign('cates', $cates);
	   		$this->display();  		
	   }

   public function ajaxAdd()
   {	
	   		// 判断人类验证是否正确
	   			$verify = I('post.verify');
	   			// var_dump($_SESSION['answer']);
	   			// var_dump($verify);
    	if($verify != $_SESSION['answer']){
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证不通过'
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	$data = I('post.');
    	$data['uid']=$_SESSION['id'];
    	$data['create_time']=time();
    	$kiss = $data['kiss_num'];//积分
    	if ($id=M('question')->add($data)) {
    	$this->autoAdd('user','kiss',"-{$kiss}",$_SESSION['id']);//积分发布问题时候减少
    			$arr = array(
    			'error'=>0,
    			'info'=>'发布成功',
    			'url'=>U('home/jie/index',array('id'=>$id))
    			);
    		echo json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'发布失败'
    			);
    		echo json_encode($arr);
    		exit();
    	}
    	

    	

  	}
 

    public function edit()
    {

		$id =I('get.id');
		$sql = "select t1.*,t2.kiss,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.id={$id} ";
		$questions = M()->query($sql);
		// var_dump($questions);
		$questions = current($questions);
		$this->assign('questions',$questions);
			 //取人类验证问题并且分配数据
	   		$res = D('verify')->getOneverify();
	   		$this->assign('res', $res);
	   		//去数据库表category的数据分配数据
	   		$cates = D('category')->getCate();
	   		$this->assign('cates', $cates);
	   		// var_dump($questions);



	   		$this->display();  	
			

	}
	//编辑更新
	   public function ajaxEdit()
   {	
			   		// 判断人类验证是否正确
			   			$verify = I('post.verify');
			   			// var_dump($_SESSION['answer']);
			   			// var_dump($verify);
		    	if($verify != $_SESSION['answer']){
		    		$arr = array(
		    			'error'=>1,
		    			'info'=>'人类验证不通过'
		    			);
		    		echo json_encode($arr);
		    		exit;
		    	}
		    	$data = I('post.');
		    	// var_dump($data);
		    	$data['uid']=$_SESSION['id'];
		    	$data['create_time']=time();
		    	$kiss = $data['kiss_num'];
		    	if (M('question')->save($data)) {
		    		$this->autoAdd('user','kiss',"-{$kiss}",$_SESSION['id']);
		    			$arr = array(
		    			'error'=>0,
		    			'info'=>'更新成功',
		    			'url'=>U('home/jie/index',array('id'=>$data['id']))
		    			);
		    		echo json_encode($arr);
		    	}else{
		    		$arr = array(
		    			'error'=>1,
		    			'info'=>'更新失败'
		    			);
		    		echo json_encode($arr);
		    		exit();
		    	}
		    	


  	}
  	//帖子的收藏功能
  		public function store()
  		{
  			$qid = I('post.qid');
  			$uid = $_SESSION['id'];
  			if ($qid) {
  				if (M('store')->where(array('qid'=>$qid,'uid'=>$uid))->find()) {
  					M('store')->where(array('qid'=>$qid,'uid'=>$uid))->delete();
  					$arr = array(
		    			'error'=>3,
		    			'info'=>'取消收藏'
		    			);
		    		echo json_encode($arr);
		    		exit();
  				}else{
  					$data['qid']=$qid;
  					$data['uid']=$uid;
  					$data['create_time']=time();
  					M('store')->add($data);
  					$arr = array(
		    			'error'=>2,
		    			'info'=>'收藏成功'
		    			);
		    		echo json_encode($arr);
		    		exit();
  				}
  			}else{
  				$arr = array(
		    			'error'=>1,
		    			'info'=>'参数'
		    			);
		    		echo json_encode($arr);
		    		exit();
  			}
  		}
  		//搜索功能
  		//基于中文分词
  		public function So()
  		{	
  			header("Content-type: text/html; charset=utf-8");
  			$q = I('get.q');
  			// require '/path/to/pscws4.class.php';
  			Vendor('Pscws4.pscws4','','.class.php');
  			$pscws = new \PSCWS4('utf8');

	        $pscws->set_dict('./Public/dict.utf8.xdb');

	        $pscws->send_text($q);

	        $tops = $pscws->get_tops(3, 'n,v');
	        // var_dump($tops);
	        $sql = "select * from question where ";
	        if (empty($tops)) {
	        	$this->display();
	        	exit();
	        }

	        foreach ($tops as $row) {
	            $sql .= " title like '%".$row['word']."%' or";
	        }

	        $sql =  rtrim($sql,'or');


	        $res = M()->query($sql);
	        //分页
	        $count=count($res);
	        $Page = new \Think\Page($count,3);
	        $sql = $sql." limit ".$Page->firstRow.",".$Page->listRows;
	        $res = M()->query($sql);
	        // var_dump($sql);exit();
			$show = $Page->show();
	        
	        foreach ($res as $key =>  $row) {
	        	foreach ($tops as $value) {
	        		
	        	
	        	$res[$key]['title']=str_replace($value['word'], "<span style='color:red;background-color:#fff;'>{$value['word']}</span>", $row['title'] );
	        		$row['title']=$res[$key]['title'];
	        	
	        	}

	        	// $res[$key]['face']
	        	$res[$key]['face']=M('user')->where(array('id'=>$row['uid']))->getField('face');
	        }

	       // var_dump($res);未分页

			
			$this->assign('page',$show);
	       $this->assign('res',$res);
	       layout('top');
	       $this->display();

	  	}
 
	  	//采纳未结帖子的方法
	  	public function status()
	  	{
	  		$id = I('post.status');
	  		
	  		$status = M('answer')->where(array('id'=>$id))->getField('status');

	  		$uid = M('answer')->where(array('id'=>$id))->getField('uid');
	  		$qid = M('answer')->where(array('id'=>$id))->getField('qid');
	  		$kiss = M('question')->where(array('id'=>$qid))->getField('kiss_num');
	  		// if ($uid==$_SESSION['id']) {
	  		// 	exit();
	  		// }
	  		if ($status==0) {
	  			
	  			M('answer')->where(array('id'=>$id))->save(array('status'=>1));
	  			M('question')->where(array('id'=>$qid))->save(array('status'=>"已采纳"));
	  			// $this->autoAdd('user','kiss',"-{$kiss}",$_SESSION['id']);
	  			$this->autoAdd('user','kiss',"+{$kiss}",$uid);
	  				$arr = array(
		    			'error'=>1,
		    			'info'=>'已经采纳',
		    			'url'=>U('home/jie/index',array('id'=>$qid))
		    			);
		    		echo json_encode($arr);
		    		exit();
	  		
	  	}else{

	  		$arr = array(
		    			'error'=>0,
		    			'info'=>'错误'
		    			);
		    		echo json_encode($arr);
		    		exit();


	  	}
	 }


	 //点赞功能

	 public function zan()
  		{
  			$aid = I('post.aid');
  			$uid = $_SESSION['id'];
  			$data['aid']=$aid;
  			$data['uid']=$uid;
  			
  			if ($aid) {
  				if ($res = M('zan')->where($data)->find()) {
  					M('zan')->where($data)->delete();
  					$this->autoAdd('answer','zan_num','-1',$aid);
  					$arr = array(
		    			'error'=>3,
		    			'info'=>'取消点赞'
		    			);
  					// $this->assign('res',$res['aid'])
		    		echo json_encode($arr);
		    		exit();
  				}else{
  					$data['create_time']=time();
  					M('zan')->add($data);
  					$this->autoAdd('answer','zan_num','+1',$aid);
  					$arr = array(
		    			'error'=>2,
		    			'info'=>'点赞成功'
		    			);
		    		echo json_encode($arr);
		    		exit();
  				}
  			}else{
  				$arr = array(
		    			'error'=>1,
		    			'info'=>'参数没有啊'
		    			);
		    		echo json_encode($arr);
		    		exit();
  			}
  		}




}








 ?>